package pkg

import (
	"everyone/C/rpc_C/appconfig"
	"fmt"
	"github.com/smartwalle/alipay/v3"
)

type Alipay interface {
	Pay(Subject, OutTradeNo, TotalAmount string)
}
type Alipays struct {
	PrivateKey string
	AppId      string
	NotifyURL  string
	ReturnURL  string
}

func NewAlipay() *Alipays {
	return &Alipays{
		PrivateKey: appconfig.ViperData.Alipay.PrivateKey,
		AppId:      appconfig.ViperData.Alipay.AppId,
		NotifyURL:  appconfig.ViperData.Alipay.NotifyURL,
		ReturnURL:  appconfig.ViperData.Alipay.ReturnURL,
	}
}

func (a *Alipays) Pay(Subject, OutTradeNo, TotalAmount string) string {
	var privateKey = a.PrivateKey // 必须，上一步中使用 RSA签名验签工具 生成的私钥
	client, err := alipay.New(a.AppId, privateKey, false)
	if err != nil {
		fmt.Println(err)
	}
	var p = alipay.TradeWapPay{}
	p.NotifyURL = a.NotifyURL
	p.ReturnURL = a.ReturnURL
	p.Subject = Subject
	p.OutTradeNo = OutTradeNo
	p.TotalAmount = TotalAmount
	p.ProductCode = "QUICK_WAP_WAY"

	url, err := client.TradeWapPay(p)
	if err != nil {
		fmt.Println(err)
	}
	var payURL = url.String()
	fmt.Println(payURL)
	return payURL
}
